package pre.bookcloud.java21.chapter15;

import java.awt.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class SignInSystem extends JFrame {
  private List<Student> studentList;
  private JPanel signInPanel;
  private JLabel dateLabel;
  private JButton todaySignInButton;
  private JButton viewHistoryButton;
  private JButton statisticsButton;

  public SignInSystem() {
    setTitle("学习签到系统");
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    // 初始化数据
    initData();

    // 创建UI组件
    createUI();
  }

  private void initData() {
    studentList = new ArrayList<>();

    // 生成模拟数据，按照界面显示的学生名单
    String[] studentNames = {
      "张子牧", "那田欣", "赵雅楠", "徐翃高", "徐诗尧", "汪子扬",
      "蒋孙扬", "包浩鸣", "陈慕岩", "张子益", "谢释辰", "王泽涵",
      "卜同学", "李泽宁", "嵇奕宇", "薛智予", "曹羽墨", "方钧喆",
      "戴锦瑞", "马芸晅", "徐晨菲", "徐国恩", "朱祥华", "朱蓓蓓"
    };

    LocalDate today = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String dateString = today.format(formatter);

    for (int i = 0; i < studentNames.length; i++) {
      Student student = new Student();
      student.setName(studentNames[i]);
      student.setStuNo("202506" + String.format("%03d", i + 1));
      student.setStatus(Math.random() > 0.3 ? 1 : 0); // 70%概率已签到
      student.setSignDate(dateString);
      student.setGenDate(dateString);
      studentList.add(student);
    }
  }

  private void createUI() {
    // 创建顶部按钮面板
    JPanel buttonPanel = new JPanel();
    todaySignInButton = new JButton("今日签到");
    viewHistoryButton = new JButton("查看历史签到");
    statisticsButton = new JButton("统计");

    buttonPanel.add(todaySignInButton);
    buttonPanel.add(viewHistoryButton);
    buttonPanel.add(statisticsButton);

    // 创建日期标签
    LocalDate today = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
    String formattedDate = today.format(formatter);
    dateLabel = new JLabel(formattedDate + "签到", JLabel.CENTER);
    dateLabel.setFont(new Font("宋体", Font.BOLD, 24));

    // 创建签到面板
    signInPanel = new JPanel();
    signInPanel.setLayout(new GridLayout(4, 6, 10, 10)); // 4行6列的网格布局
    signInPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    // 添加学生按钮到签到面板
    updateSignInPanel();

    // 添加底部版权信息
    JLabel copyrightLabel = new JLabel("晨文控制作. 2025", JLabel.RIGHT);
    copyrightLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 15));

    // 设置主面板布局
    setLayout(new BorderLayout());
    add(buttonPanel, BorderLayout.NORTH);
    add(dateLabel, BorderLayout.NORTH);
    add(signInPanel, BorderLayout.CENTER);
    add(copyrightLabel, BorderLayout.SOUTH);

    // 添加按钮事件监听器
    addEventListeners();
  }

  private void updateSignInPanel() {
    signInPanel.removeAll();

    for (Student student : studentList) {
      JButton studentButton = new JButton(student.getName());
      studentButton.setPreferredSize(new Dimension(100, 40));
      studentButton.setFont(new Font("宋体", Font.PLAIN, 14));

      // 根据签到状态设置按钮样式
      if ("已签到".equals(student.getStatus())) {
        studentButton.setBackground(Color.LIGHT_GRAY);
        studentButton.setEnabled(false);
      } else {
        studentButton.setBackground(Color.WHITE);
        studentButton.addActionListener(
            e -> {
              // 点击签到
              student.setStatus(1);
              studentButton.setBackground(Color.LIGHT_GRAY);
              studentButton.setEnabled(false);
              JOptionPane.showMessageDialog(this, student.getName() + "签到成功！");
            });
      }

      signInPanel.add(studentButton);
    }

    signInPanel.revalidate();
    signInPanel.repaint();
  }

  private void addEventListeners() {
    todaySignInButton.addActionListener(
        e -> {
          // 刷新今日签到界面
          LocalDate today = LocalDate.now();
          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
          dateLabel.setText(today.format(formatter) + "签到");
          updateSignInPanel();
        });

    viewHistoryButton.addActionListener(
        e -> {
          JOptionPane.showMessageDialog(this, "历史签到功能待实现");
        });

    statisticsButton.addActionListener(
        e -> {
          JOptionPane.showMessageDialog(this, "统计功能待实现");
        });
  }

  public static void main(String[] args) {
    // 在事件调度线程中启动应用程序
    SwingUtilities.invokeLater(
        () -> {
          SignInSystem signInSystem = new SignInSystem();
          signInSystem.setVisible(true);
        });
  }
}
